import axios from "@/utils/axios";
import { onMounted, ref } from "vue";
import moment from 'moment';

export default () => {
    const lineOption: any = ref(null);
    const barOption: any = ref(null);
    let getLateryYield = () => {
        axios
            .post("/mix/data/getLateryYield")
            .then((res) => {
                // 拌合站数组,产量数组,日期数组
                let mixList = new Array();
                let dayList = new Array();
                let yieldList = new Array();
                let yieldTotalList = new Array();
                res.data.forEach((item1: any) => {
                    let yieldItemList = new Array();
                    let indayList = new Array();

                    item1.analyzeList.forEach((item2: any) => {
                        if (yieldList.length == 0) indayList.push(item2.day);
                        yieldItemList.push(item2.yield.yield);
                    })
                    if (indayList.length > dayList.length) dayList = indayList;
                    mixList.push(item1.name);
                    yieldList.push({
                        name: item1.name,
                        type: 'line',
                        data: yieldItemList
                    })
                    yieldTotalList.push(item1.yieldTotal.toFixed(2));
                })
                lineOption.value = {
                    title: {
                        top: '12px',
                        left: '12px',
                        text: '产量统计(折线图)'
                    },
                    tooltip: {
                        trigger: 'axis'
                    },
                    legend: {
                        data: mixList,
                        bottom: '12px'
                    },
                    xAxis: {
                        type: 'category',
                        data: dayList,
                        boundaryGap: false,
                    },
                    yAxis: {
                        type: 'value'
                    },
                    series: yieldList
                };
                barOption.value = {
                    title: {
                        top: '12px',
                        left: '12px',
                        text: '产量统计(柱状图)'
                    },
                    tooltip: {
                        trigger: 'axis',
                    },
                    xAxis: {
                        type: 'category',
                        data: mixList
                    },
                    yAxis: {
                        type: 'value'
                    },
                    series: [{
                        type: 'bar',
                        data: yieldTotalList,
                        showBackground: true,
                        backgroundStyle: {
                            color: 'rgba(220, 220, 220, 0.8)'
                        }
                    }]
                };
            })
    }
    /**
       * 获取生产数据
       * @param startTime 开始时间
       * @param endTime 结束时间
       */
    let getBarInfoBydata = (startTime: string, endTime: string) => {
        if (typeof startTime == 'object') startTime = moment(startTime).format("YYYY-MM-DD") + " 00:00:00";
        if (typeof endTime == 'object') endTime = moment(endTime).format("YYYY-MM-DD") + " 23:59:59";
        axios
            .post("/mix/data/getAllYieldBYDate", {
                startTime: startTime,
                endTime: endTime
            })
            .then((res) => {
                // 拌合站数组,产量数组,日期数组
                let mixList = new Array();
                let dayList = new Array();
                let yieldList = new Array();
                let yieldTotalList = new Array();
                res.data.forEach((item1: any) => {
                    let yieldItemList = new Array();
                    let indayList = new Array();

                    item1.analyzeList.forEach((item2: any) => {
                        if (yieldList.length == 0) indayList.push(item2.day);
                        yieldItemList.push(item2.yield);
                    })
                    if (indayList.length > dayList.length) dayList = indayList;
                    mixList.push(item1.name);
                    yieldList.push({
                        name: item1.name,
                        type: 'line',
                        data: yieldItemList
                    })
                    yieldTotalList.push(item1.yieldTotal.toFixed(2));
                })
                barOption.value = {
                    title: {
                        top: '12px',
                        left: '12px',
                        text: '产量统计(柱状图)'
                    },
                    tooltip: {
                        trigger: 'axis',
                    },
                    xAxis: {
                        type: 'category',
                        data: mixList
                    },
                    yAxis: {
                        type: 'value'
                    },
                    series: [{
                        type: 'bar',
                        data: yieldTotalList,
                        showBackground: true,
                        backgroundStyle: {
                            color: 'rgba(220, 220, 220, 0.8)'
                        }
                    }]
                };
            })
    }
    onMounted(() => {
        getLateryYield()
    })

    return {
        lineOption,
        barOption,
        getBarInfoBydata
    }
}